<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Actions\Row\WithdrawalApproveAction;
use App\Admin\Actions\Row\WithdrawalRejectAction;
use App\Admin\Repositories\Withdrawal;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Arr;

class WithdrawalController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Withdrawal(['bankCard', 'user']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->column('id')->sortable();
            $grid->column('user.name', '申请人姓名');
            $grid->column('user.telephone', '申请人电话');
            $grid->column('amount');
            $grid->column('fee');
            $grid->column('arrival_amount');
            $grid->column('status', '状态')->using([
                1 => '待审核',
                2 => '审核通过',
                3 => '已驳回',
            ]);
            $grid->column('applied_at');
            $grid->column('processed_at');
//            $grid->column('user_bank_card_id');
            $grid->column('bankCard.bank_name', '银行名称');
            $grid->column('bankCard.bank_branch', '支行名称');
            $grid->column('bankCard.card_type', '银行卡类型')->using([
                1 => '借记卡',
                2 => '信用卡',
                3 => '对公账户',
            ]);
            $grid->column('bankCard.card_number', '银行卡号');
            $grid->column('bankCard.card_holder', '持卡人姓名');
            $grid->column('bankCard.id_card_number', '证件号码');
            $grid->column('bankCard.phone', '预留手机号');

            $grid->export()->xlsx()->rows(function ($rows) {
                return $this->dataProcessing($rows);
            });

            $grid->column('audit_admin');
            $grid->column('audited_at');
            $grid->column('reject_reason');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // 移除默认操作
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();

                // 仅当状态为待审核时显示按钮
                if ($actions->row->status === 1) {
                    $actions->append(new WithdrawalApproveAction());
                    $actions->append(new WithdrawalRejectAction());
                }
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.name', '申请人姓名');
                $filter->equal('user.telephone', '申请人电话');
                $filter->equal('status', '状态')->select([
                    1 => '待审核',
                    2 => '审核通过',
                    3 => '已驳回',
                ]);
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Withdrawal(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('amount');
            $show->field('fee');
            $show->field('arrival_amount');
            $show->field('status');
            $show->field('applied_at');
            $show->field('processed_at');
            $show->field('user_bank_card_id');
            $show->field('audit_admin');
            $show->field('audited_at');
            $show->field('reject_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Withdrawal(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('amount');
            $form->text('fee');
            $form->text('arrival_amount');
            $form->text('status');
            $form->text('applied_at');
            $form->text('processed_at');
            $form->text('user_bank_card_id');
            $form->text('audit_admin');
            $form->text('audited_at');
            $form->text('reject_reason');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    private function dataProcessing($rows)
    {
        foreach ($rows as $index => &$row) {
            $row['bankCard.bank_name'] = $row->bankCard->bank_name;
            $row['bankCard.bank_branch'] = $row->bankCard->bank_branch;
            $row['bankCard.card_type'] = $row->bankCard->card_type;
            $row['bankCard.card_number'] = $row->bankCard->card_number;
            $row['bankCard.card_holder'] = $row->bankCard->card_holder;
            $row['bankCard.id_card_number'] = $row->bankCard->id_card_number;
            $row['bankCard.phone'] = $row->bankCard->phone;
            $row['bankCard.card_type'] = Arr::get([
                1 => '借记卡',
                2 => '信用卡',
                3 => '对公账户',
            ], $row->bankCard->card_type);
        }
        return $rows;
    }
}
